<?php

namespace App\Models\Admin;

use App\Models\BaseModel;
use App\Common\ValidateHandler;
use App\Common\AliossHandler;
use Auth;

class Notice extends BaseModel
{
    protected $fillable = [
        'id', 'store_id', 'title', 'cover', 'detail',
        'status', 'begin_at', 'end_at', 'created_at', 'updated_at'
    ];

    public function getList($pageSize)
    {
        $storeId = Auth::guard('admin')->user()->store_id;
        if ($storeId == 0) {
            // 管理员查看全部店铺公告
            $data = $this->orderBy('store_id')->paginate($pageSize)->toArray();
        } else {
            // 店家只能查看自己店的公告
            $data = $this->where('store_id', $storeId)->orderBy('store_id')->paginate($pageSize)->toArray();
        }
        $data = $this->handleData($data);

        return $this->success($data);
    }

    private function handleData($data)
    {
        foreach ($data['data'] as $k => $notice) {
            $data['data'][$k]['cover'] = AliossHandler::read('notice', $notice['cover']);
            $data['data'][$k]['origin'] = $notice['cover'];
            $data['data'][$k]['begin_at'] = date("Y-m-d", strtotime($notice['begin_at']));
            $data['data'][$k]['end_at'] = date("Y-m-d", strtotime($notice['end_at']));
        }

        return $data;
    }

    public function handle($data)
    {
        $arr = [];
        $id = $data['id'];

        $arr['title'] = $data['title'];
        $arr['cover'] = $data['cover'];
        $arr['detail'] = $data['detail'];
        $arr['begin_at'] = $data['begin_at'];
        $arr['end_at'] = $data['end_at'];
        $arr['status'] = $data['status'];

        $userStoreId = Auth::guard('admin')->user()->store_id; //用户店铺id 0为管理员
        if ($userStoreId == 0) {
            // 管理员提交
            $arr['store_id'] = empty($data['store_id']) ? 0 : $data['store_id'];
            if ($arr['store_id'] == 0) return $this->error(['info' => '请选择店铺']);
        } else {
            $arr['store_id'] = $userStoreId;
        }

        $result = $this->validateData($arr);
        if (!$result['res'])
            return $this->error($result);

        if ($id == 'new') {
            $this->create($arr);
        } else {
            $this->where('id', $id)->update($arr);
        }

        return $this->success(['info' => '操作成功']);
    }

    private function validateData($data)
    {
        $arr = [
            0 => [
                'name' => '公告标题',
                'value' => $data['title'],
                'func' => 'is_empty'
            ],
            1 => [
                'name' => '公告封面',
                'value' => $data['cover'],
                'func' => 'is_empty'
            ],
            2 => [
                'name' => '公告详情',
                'value' => $data['detail'],
                'func' => 'is_empty'
            ],
            3 => [
                'name' => '开始时间',
                'value' => $data['begin_at'],
                'func' => 'is_empty'
            ],
            4 => [
                'name' => '结束时间',
                'value' => $data['end_at'],
                'func' => 'is_empty'
            ]
        ];

        return ValidateHandler::do($arr);
    }

    public function deleteNotice($data)
    {
        $id = $data['id'];
        $this->where('id', $id)->delete();

        return $this->success(['info' => '删除成功']);
    }
}
